<?php
//
//  INDEX PAGE
//


$is_search = false;
$paged = 0; 
$offset = 0; 


if ( isset( $_GET['action'])) {
    switch( $_GET['action'] )  {
        case 'search':
                $is_search = true;
                $total_pages = 1;
                
                $subscription_set = Subscription::advanced_search( array(
                        'status' => isset( $_GET['status'] ) ? $_GET['status'] : null,
                        'displayname' => isset( $_GET['displayname'] ) ? $_GET['displayname'] : null,
                        'city' => isset( $_GET['city'] ) ? $_GET['city'] : null,
                        'province' => isset( $_GET['province'] ) ? $_GET['province'] : null,
                        'search_date' => isset( $_GET['search_date_type'] ) ? 
                                array( $_GET['search_date_type'], $_GET['search_from'], $_GET['search_to'] )
                                : null
                ));
                $total_subscriptions = count($subscription_set);
                if ($total_subscriptions == 1) $Controller->redirect_to('edit', array('user' => $subscription_set[0]->user->user_id) );
            break;

    }
} else {
    
    
    // Pagination
    if ( isset($_GET['paged']) ) {
        $paged = $_GET['paged'];  
        $offset = $_GET['paged'] * $session->setting_records_per_page;
    }
            
    // Sorting
    if ( isset($_GET['sort']) ) {
       $sort =  $_GET['sort'];
       $order = ( isset($_GET['order']) ) ? strtoupper($_GET['order']) : 'DESC';
       $sort_by = "ORDER BY {$sort} {$order}";
    } else { $sort_by = ""; }
    
    // filtering
    $filter = ( isset($_GET['filter']) ) ? $_GET['filter'] : '';
    if ( $filter != 'expired' ) {
        $filter_query = ( isset($_GET['filter']) ) ? "AND " .Subscription::get_tablename(). ".status='$filter'" : "AND " .Subscription::get_tablename(). ".status!='inactive'";
        $total_subscriptions = Subscription::get_total_subscriptions( $filter ); 
        $total_pages = ceil($total_subscriptions/$session->setting_records_per_page);
        $subscription_set = Subscription::find_by_publication($session->publication->publication_id, $offset, $filter_query, $sort_by);
    } else {
        $subscription_set = Subscription::find_by_recently_expired($session->publication->publication_id);
        $total_subscriptions = count(  $subscription_set  );
        $total_pages = 1;
    }
    
    
    
}
require_once('header.php');
?> 

    
    
	<div id="subscription-date-info" class="padding left white-trans rounded-small ">
        <h2 id="subscription-week-title">Subscriptions for, <?php echo date('l, F j, Y', get_date_from_weeknum( get_weeknum()) ); ?> - <small>(Week #<?php echo get_weeknum(); ?>)</small></h2>
    </div>
	<section class="clear">
	    <div id="main-tabs">   
	        <header>
    	    <nav id="tabs">
    	    <ul class="group">
    	       <?php if ( $is_search ) { ?>
               <li class="grey button left active"><a href="<?php echo $Controller->page_url();?>&search=<?php echo $_GET['search']; ?>"><?php echo $total_subscriptions; ?> Results</a></li>
    	       <?php } ?>
    	       <li class="grey button left <?php echo ( $filter == '' && !$is_search ) ? 'active' : ''; ?>"><a href="<?php echo $Controller->page_url();?>"><?php echo Subscription::get_total_subscriptions(); ?> Total</a></li>
    	       <li class="grey button left <?php echo ( $filter == 'active' ) ? 'active' : ''; ?>"><a href="<?php echo $Controller->page_url();?>&filter=active"> <?php echo Subscription::get_total_subscriptions("active"); ?> Active</a></li>
               <li class="grey button left <?php echo ( $filter == 'paused') ? 'active' : ''; ?>"><a href="<?php echo $Controller->page_url();?>&filter=paused"> <?php echo  Subscription::get_total_subscriptions("paused"); ?> Paused</a></li> 
               <li class="grey button left <?php echo ( $filter == 'future') ? 'active' : ''; ?>"><a href="<?php echo $Controller->page_url();?>&filter=future"> <?php echo  Subscription::get_total_subscriptions("future"); ?> Future</a></li> 
               <li class="grey button left <?php echo ( $filter == 'expiring') ? 'active' : ''; ?>"><a href="<?php echo $Controller->page_url();?>&filter=expiring"> <?php echo Subscription::get_total_subscriptions("expiring"); ?> Expiring</a></li> 
               <li class="grey button left <?php echo ( $filter == 'expired') ? 'active' : ''; ?>"><a href="<?php echo $Controller->page_url();?>&filter=expired"> Recently Expired</a></li> 
    	       <?php  include('template.user-search-form.php'); ?>
    	    </ul> 
    	    </nav>
    	    </header>
    	    
            <?php 
            include('table.subs.php'); 
            ?>

	    </div>
	</section>

<?php
//
//  End page.
//
require_once('footer.php');
?>




